/**
 * 
 */
package id.ac.gunadarma.paracorp.viewmodel;

import id.ac.gunadarma.paracorp.model.EnglishSentence;
import id.ac.gunadarma.paracorp.services.EnglishSentenceService;

import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.event.PagingEvent;

/**
 * @author rezalesmana
 *
 */
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class IndexVM {
	
	private List<EnglishSentence> listSentences;
	
	private EnglishSentence selectedItem;
	
	private long totalSize = 0;
	
	private int activePage = 0;
	
	private int pageSize = 0;
	
	private String selectedLanguage = "ID";

	@WireVariable
	private EnglishSentenceService englishSentenceService;
	
	@Init
	@NotifyChange({"totalSize", "listSentences"})
	public void init(@BindingParam("pageSize") int pageSize) {
		this.setPageSize(pageSize);
		this.reloadPage();
	}

	@Command
	@NotifyChange({"totalSize", "listSentences"})
	public void loadPage(@ContextParam(ContextType.TRIGGER_EVENT) PagingEvent event) {
		this.setActivePage(event.getActivePage());
		this.reloadPage();
	}
	
	@GlobalCommand
	@NotifyChange({"totalSize", "listSentences"})
	public void uploadFinished() {
		this.reloadPage();
	}
	
	public void reloadPage() {
		this.setTotalSize(this.getService().getTotalSize());
		this.setListSentences(this.getService().findAll(this.getActivePage(), this.getPageSize(), this.getSelectedLanguage()));
	}
	
	public List<EnglishSentence> getListSentences() {
		return listSentences;
	}

	public void setListSentences(List<EnglishSentence> listSentences) {
		this.listSentences = listSentences;
	}

	public EnglishSentence getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(EnglishSentence selectedItem) {
		this.selectedItem = selectedItem;
	}

	public long getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(long totalSize) {
		this.totalSize = totalSize;
	}
	
	public int getActivePage() {
		return activePage;
	}

	public void setActivePage(int activePage) {
		this.activePage = activePage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public String getSelectedLanguage() {
		return selectedLanguage;
	}

	public void setSelectedLanguage(String selectedLanguage) {
		this.selectedLanguage = selectedLanguage;
	}

	private EnglishSentenceService getService() {
		return englishSentenceService;
	}
	
}
